package com.hh.core.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.core.util.StrUtil;

import com.hh.core.xss.XSSRequestWrapper;

/**
 * 
* @ClassName: HttpUtil  
* @Description: TODO(http 请求相关类)  
* @author hxn
* @date 2018年1月18日 上午10:38:48  
*
 */
public class HttpCoreUtil {

	/**
	 * 
	* @Title: getResponse  
	* @Description:  获取 HttpServletResponse 
	* @param @return    设定文件  
	* @return HttpServletResponse    返回类型  
	* @author hxn
	* @date 2018年1月18日 上午10:39:29  
	* @throws
	 */
	public static HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }
	
	/**
	 * 
	* @Title: getRequest  
	* @Description:   获取 包装防Xss Sql注入的 HttpServletRequest 
	* @param @return    设定文件  
	* @return HttpServletRequest    返回类型  
	* @author hxn
	* @date 2018年1月18日 上午10:39:31  
	* @throws
	 */
	public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return new XSSRequestWrapper(request);
    }
	
	/**
	 * 
	* @Title: assertAjax  
	* @Description:   判断当前是不是ajax请求
	* @param @return    设定文件  
	* @return boolean    返回类型  
	* @author hxn
	* @date 2018年1月22日 下午4:13:59  
	* @throws
	 */
	public static boolean assertAjax(HttpServletRequest request) {
        if (request.getHeader("x-requested-with") != null
                && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
            //如果是ajax请求响应头会有，x-requested-with
            return true;
        }else if(StrUtil.isNotEmpty(request.getHeader("Content-Type"))&&request.getHeader("Content-Type").startsWith("multipart/form-data")) {
            return true;
        }else{
            return false;
        }
    }
	
	/**
	 * 
	* @Title: getIpAddr  
	* @Description:   获取当前操作IP
	* @param @param request
	* @param @return    设定文件  
	* @return String    返回类型  
	* @author hxn
	* @date 2018年2月13日 上午9:58:55  
	* @throws
	 */
	public static String getIpAddr(HttpServletRequest request){  
        String ipAddress = request.getHeader("x-forwarded-for");  
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
            ipAddress = request.getHeader("Proxy-Client-IP");  
        }  
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
            ipAddress = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
            ipAddress = request.getRemoteAddr();  
            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){  
                //根据网卡取本机配置的IP  
                InetAddress inet=null;  
                try {  
                    inet = InetAddress.getLocalHost();  
                } catch (UnknownHostException e) {  
                    e.printStackTrace();  
                }  
                ipAddress= inet.getHostAddress();  
            }  
        }  
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15  
            if(ipAddress.indexOf(",")>0){  
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));  
            }  
        }  
        return ipAddress;   
    }
}
